import ContextMenu from '../ui/ContextMenu';
import Utils from '../utils/index';
import Config from '../config';
class GanttContextMenu {
	constructor(Gantt) {
		this.pointContextmenu = null;
		this.castPlanContextmenu = null;
		this.Gantt = Gantt;

		this.currentPoint = null;
	}

	showPointContextmenu(point, e) {
		this.currentPoint = point;
		if (this.pointContextmenu) {
			this.pointContextmenu.show(e);
		} else {
			let menuArray = Config.ifShowView?'showHeatBarMenu':'mainHeatBarMenu';
			this.pointContextmenu = new ContextMenu(Config[menuArray], (index, subIndex) => {
				if (index === undefined) {
					return false;
				}
				let code = this.pointContextmenu.items[index].code
				if (subIndex !== undefined) {
					code = this.pointContextmenu.items[index].sub[subIndex].code;
				}
				this.pointContextClick(code);
				return true;
			}, e, {
					className: 'point-context'
				});
		}

		console.log(point.extra.operationStat);
		this.pointContextmenu.disabledItem('releasePdi', point.extra.operationStat !== 'S');
		this.pointContextmenu.disabledItem('cancelPdi', point.extra.operationStat !== 'I');
		this.pointContextmenu.disabledItem('changeHotFlag', !point.extra.facilityNo.startsWith('CCM')||point.extra.operationStat !== 'S');
	}

	pointContextClick(code) {
		let point = this.currentPoint,
			args = [point.extra.heatSid, point]; //[code.startsWith('show') ? point.extra.heatSid : point];

		// 由于 insertBefore 和  insertAfter 最终是调同一个函数，所以在这里进行判断
		if (code === 'insertBefore') {
			if (
				(point.extra.operationStat !== 'S' && point.extra.operationStat !== 'I') ||
				(point.extra.facilityNo.startsWith('BOF') && Utils.facilities[Math.floor(point.y)].facilitySimpl === 'P')
			) {
				alert('不允许执行之前插入作业操作');
				return false;
			}
			args.push(true);
			code = 'insertOperation';
		} else if (code === 'insertAfter') {
			if (point.extra.facilityNo.startsWith('CCM') || point.extra.facilityNo.startsWith('SCM')) {
				alert('不允许执行之后插入作业操作');
				return false;
			}
			let points = point.series.points,
				nextPoint = null;
			if (points.length - point.index > 1) {
				nextPoint = points[point.index + 1];
				if (nextPoint.extra.operationStat !== 'S' && nextPoint.extra.operationStat !== 'I') {
					alert('不允许执行之后插入作业操作');
					return false;
				}
			} else {
				alert('不允许执行之后插入作业操作');
				return false;
			}
			code = 'insertOperation';
		} else if (code === 'heatClose' || code === 'heatCompleteOrClose') {
			if (code === 'heatCompleteOrClose') {
				args.length = 1;
			} else {
				code = 'heatCompleteOrClose';
				args[1] = true;
			}
		} else if (code === 'unscheduleHeat') {
         let heat = this.Gantt.cache.getById('heats', point.extra.heatSid);
         if (heat.heatStat !== 'S') {
            alert('不能取消炉次调度');
            return false;
         }
         args[0] = heat.sid;
		}
		this.Gantt[code](...args);
	}


	// 浇次计划右键菜单
	showCastPlanContextmenu(castSid, heatSid, heatStat, e) {
		this.castSid = castSid;
		this.heatSid = heatSid;

		if (!this.castPlanContextmenu) {
			let castMenuArray = Config.ifShowView?'showCastBarMenu':'mainCastBarMenu';
			this.castPlanContextmenu = new ContextMenu(Config[castMenuArray], (index, subIndex) => {
				if (index === undefined) {
					return false;
				}

				let code = this.castPlanContextmenu.items[index].code
				if (subIndex !== undefined) {
					code = this.castPlanContextmenu.items[index].sub[subIndex].code;
				}
				this.castPlanContextClick(code, this.castSid, this.heatSid, heatStat);
				return true;
			}, e);
		} else {
			this.castPlanContextmenu.show(e);
		}

		// disabled
		let isR = heatStat === 'R',
			isC = heatStat === 'C',
			isS = heatStat === 'S',
			isY = heatStat === 'Y';
		// this.castPlanContextmenu.disabledItem('showHeat', isC || isR);
		this.castPlanContextmenu.disabledItem('scheduleHeat', isC || !isR);
		this.castPlanContextmenu.disabledItem('scheduleHeat-default', isC || !isR);
		this.castPlanContextmenu.disabledItem('unscheduleHeat', !isS);
	}

	// 浇次计划右键菜单点击事件
	castPlanContextClick(code, castSid, heatSid, heatStat) {
		let args = [heatSid];


		if (code.startsWith('scheduleHeat')) {
			args = [castSid, heatSid];
			if (code === 'scheduleHeat-default') {
				code = 'scheduleHeat';
				args.push(true);
			}
		} else if (code === 'unscheduleHeat') {
			let heat = this.Gantt.cache.getById('heats', heatSid);
			if (heat.heatStat !== 'S') {
				alert('不能取消炉次调度');
				return false;
			}
			args.push(true);
		} else if (code === 'heatClose') {
			code = 'heatCompleteOrClose';
			args.push(true);
		}
		this.Gantt[code](...args);
	}



}

export default GanttContextMenu;
